<template>
  <div class="my-cover">
    <!-- 图片按钮 -->
    <div class="btn_img" @click="openDialog">
      <img :src="value || imgSrc" />
    </div>
    <!-- 对话框 -->
    <el-dialog
      title="选择封面"
      :append-to-body="true"
      :visible.sync="dialogVisible"
      width="720px">
      <!-- tab组件 -->
      <el-tabs v-model="activeName" type="card">
        <el-tab-pane label="素材库" name="image">
          <div class="top-box">
            <el-radio-group size="medium" v-model="collect" @change="changeCollect">
              <el-radio-button :label="false">全部</el-radio-button>
              <el-radio-button :label="true">收藏</el-radio-button>
            </el-radio-group>
          </div>
          <el-row :gutter="10" class="img_list">
            <el-col
            class="img_item "
            :class="{ selected:  selectedImageUrl=== item.url}"
            @click.native="selected(item)"
             v-for="item in images" :key="item.id"
            :xs="12" :sm="8" :md="6" :lg="4" :xl="4">
              <el-image style="height: 100px; width: 100%" :src="item.url" fit="cover">
              </el-image>
            </el-col>
          </el-row>
          <el-pagination
           @current-change='handleCurrentChange'
            layout="prev, pager, next"
            background
            :page-size="6"
            :current-page="current_page"
            :total="total_count">
          </el-pagination>
        </el-tab-pane>

        <el-tab-pane label="上传图片" name="upload">
          <el-upload
          class="avatar-uploader"
          :action="$http.defaults.baseURL + '/mp/v1_0/user/images'"
          :show-file-list="false"
          :headers="headers"
          name="image"
          :on-success="handleSuccess">
          <el-image :src="imageUrl" v-if="imageUrl" class="avatar"></el-image>
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-tab-pane>
      </el-tabs>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { reqGetPictures } from '@/api/pictures.js'
import { getUser } from '@/utils/storage.js'
import DefaultImg from '@/assets/default.png'
export default {
  name: 'MyCover',
  data () {
    return {
      // 控制对话框显示隐藏
      dialogVisible: false,
      // 当前tabs组件激活的选项卡的name属性的值
      activeName: 'image',
      images: [],
      current_page: 1, // 当前页
      total_count: 0, // 总条数
      collect: false, // 是否收藏
      per_page: 4, // 每页显示条数
      selectedImageUrl: '', // 选中的图片的地址
      imageUrl: '', // 上传图片的地址
      imgSrc: DefaultImg, // 预览展示的图片
      headers: {
        Authorization: `Bearer ${getUser().token}`
      }
    }
  },
  props: ['value'],
  created () {
    this.getPictures()
  },
  methods: {
    openDialog () {
      this.dialogVisible = true
    },
    async getPictures () {
      try {
        const res = await reqGetPictures({
          per_page: this.per_page,
          page: this.current_page,
          collect: this.collect
        })
        this.images = res.data.data.results
        this.total_count = res.data.data.total_count
      } catch (err) {
        console.log(err)
      }
    },
    handleCurrentChange (index) {
      this.current_page = index
      this.getPictures()
    },
    changeCollect () {
      this.current_page = 1
      this.getPictures()
    },
    selected (item) {
      // console.log(item)
      this.selectedImageUrl = item.url
    },
    handleSuccess (res, file) {
      // console.log('文件上传成功了', res)
      this.imageUrl = res.data.url
      this.$message.success('上传成功')
    },
    // 将点击了 的图片进行预览
    handleConfirm () {
      // 素材库的地址
      if (this.activeName === 'image') {
        if (!this.selectedImageUrl) {
          this.$message.error('请选择图片')
          return
        }
        this.imgSrc = this.selectedImageUrl
        // console.log(this.selectedImageUrl)
      } else if (this.activeName === 'upload') {
        // 上传图片的地址
        if (!this.imageUrl) {
          this.$message.error('请上传图片')
          return
        }
        this.imgSrc = this.imageUrl
        // console.log(this.imageUrl)
      }
      // 子传父  将图片地址传输出去
      this.$emit('input', this.imgSrc)
      // 关闭对话框
      this.dialogVisible = false
    }
  }
}
</script>

<style scoped lang='less'>
// 图片按钮
.btn_img {
  width: 150px;
  height: 150px;
  border: 1px dashed #ddd;
  img {
    width: 100%;
    height: 100%;
    display: block;
  }}
  // 素材列表
.img_list {
  margin-top: 15px;
  .img_item{
    width: 150px;
    height: 120px;
    border: 1px dashed #ddd;
    display: inline-block;
    margin-right: 15px;
    position: relative;
    img {
      width: 100%;
      height: 100%;
      display: block;
    }
  }
}

// <div class="selected"></div>
.selected::after{
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.3) url(../assets/selected.png) no-repeat center / 50px 50px;
}
.avatar-uploader {
    text-align: center;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}
.avatar-uploader /deep/ .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

</style>
